# -*- coding: utf-8 -*-

import json
import datetime
import decimal
from bson import ObjectId

'''
    把数据库查询到的数据转换为json格式的工具类
'''


class MyJsonEncoder(json.JSONEncoder):
    '''
        重写构造json类，对日期等格式进行特殊处理
    '''

    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime('%Y-%m-%d')
        elif isinstance(obj, decimal.Decimal):
            return str(obj)
        elif isinstance(obj, ObjectId):
            return str(obj)
        else:
            return json.JSONEncoder.default(self, obj)


class Table2JsonLuo(object):
    '''
        工具类，把MySQL数据库获取的数据转换为JSON格式
    '''


    @staticmethod
    def view_itemAndDept_json(list):
        data = []

        for row in list:
            result ={}
            result['itemId'] = row[0]
            result['deptId'] = row[1]
            result['itemHandlerName'] = row[2]
            result['itemHandlerPhone'] = row[3]
            result['itemTitle'] = row[4]
            result['itemPriceRange'] = row[5]
            result['itemContent'] = row[6]
            result['itemUrl'] = row[7]
            result['itemDeadline'] = row[8]
            result['itemPublishdate'] = row[9]
            result['itemAddress'] = row[10]
            result['itemStatus'] = row[11]
            result['serviceId'] = row[12]
            result['itemEnterPerson'] = row[13]
            result['itemEnterTime'] = row[14]
            result['checkStatusOne'] = row[15]
            result['checkRemarkOne'] = row[16]
            result['checkPersonOne'] = row[17]
            result['checkTimeOne'] = row[18]
            result['checkStatusTwo'] = row[19]
            result['checkRemarkTwo'] = row[20]
            result['checkPersonTwo'] = row[21]
            result['checkTimeTwo'] = row[22]

            result['deptName'] = row[23]
            result['deptPcode'] = row[24]
            result['deptCode'] = row[25]
            result['deptIsLock'] = row[26]
            result['deptSort'] = row[27]
            result['deptAddress'] = row[28]
            result['leveCode'] = row[29]
            result['provinceId'] = row[30]
            result['cityId'] = row[31]
            result['districtId'] = row[32]
            result['deptUrl'] = row[33]
            result['desUrl'] = row[34]
            result['extractFrom'] = row[35]
            result['extractDate'] = row[36]






            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def view_orderAndDept_json(list):
        data = []

        for row in list:
            result = {}
            result['order_id'] = row[0]
            result['order_no'] = row[1]
            result['order_ip'] = row[2]
            result['order_type'] = row[3]
            result['trade_no'] = row[4]

            result['user_id'] = row[5]
            result['user_name'] = row[6]
            result['payment_id'] = row[7]
            result['payment_fee'] = row[8]
            result['payment_status'] = row[9]  ######

            result['payment_time'] = row[10]
            result['order_created_time'] = row[11]
            result['is_invoice'] = row[12]
            result['invoice_title'] = row[13]
            result['invoice_taxes'] = row[14]

            result['payable_amount'] = row[15]
            result['real_amount'] = row[16]
            result['order_amount'] = row[17]
            result['order_point'] = row[18]
            result['is_member'] = row[19]

            result['member_name'] = row[20]
            result['member_email'] = row[21]
            result['member_telephone'] = row[22]
            result['member_province'] = row[23]
            result['member_city'] = row[24]

            result['member_district'] = row[25]
            result['member_address'] = row[26]
            result['is_coupon'] = row[27]
            result['coupon_name'] = row[28]
            result['coupon_code'] = row[29]

            result['coupon_type'] = row[30]
            result['coupon_conditions'] = row[31]
            result['coupon_discount'] = row[32]
            result['service_content'] = row[33]
            result['service_contract'] = row[34]

            result['service_code'] = row[35]
            result['service_name'] = row[36]
            result['service_price'] = row[37]
            result['service_rate'] = row[38]
            result['item_name'] = row[39]

            result['item_content'] = row[40]
            result['item_url'] = row[41]
            result['item_address'] = row[42]
            result['item_publishdate'] = row[43]
            result['item_deadline'] = row[44]

            result['item_handlername'] = row[45]
            result['item_handlerphone'] = row[46]
            result['dept_id'] = row[47]
            result['order_status'] = row[48]
            result['order_product_is_success'] = row[49]
            result['order_message'] = row[50]

            result['dept_name'] = row[51]
            result['dept_pcode'] = row[52]
            result['dept_code'] = row[53]
            result['dept_is_lock'] = row[54]
            result['dept_sort'] = row[55]
            result['dept_address'] = row[56]
            result['leve_code'] = row[57]
            result['province_id'] = row[58]
            result['city_id'] = row[59]
            result['province_id'] = row[60]
            result['dept_url'] = row[61]
            result['des_url'] = row[62]
            result['extract_from'] = row[63]
            result['extract_date'] = row[64]
            result['item_id'] = row[65]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData